package com.ideal.service.access.impl;

import com.ideal.config.Constant;
import com.ideal.dto.DictInfoDto;
import com.ideal.dto.config.AvailableConfig;
import com.ideal.enums.FileTypeEnum;
import com.ideal.model.Config;
import com.ideal.model.ConfigVersion;
import com.ideal.service.IConfigService;
import com.ideal.service.IConfigVersionService;
import com.ideal.service.ISysTreeDictService;
import com.ideal.service.access.ExeclAccessService;
import com.ideal.service.access.FileCommonAccessService;
import com.ideal.service.access.IniAccessService;
import com.ideal.service.access.PropertiesAccessService;
import com.ideal.util.file.ExeclUtil;
import com.ideal.util.file.IniUtil;
import com.ideal.util.file.PropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author songfz
 * @Description
 * @Date 2022/8/5
 */
@Service
public class FileCommonAccessServiceImpl implements FileCommonAccessService {

    @Autowired
    private IConfigVersionService configVersionService;

    @Autowired
    private IConfigService configService;

    @Autowired
    private ExeclAccessService execlAccessService;

    @Autowired
    private ISysTreeDictService sysTreeDictService;

    @Autowired
    private IniAccessService iniAccessService;

    @Autowired
    private PropertiesAccessService propertiesAccessService;


    /**
     * 有效配置加入缓存
     *
     * @param configId
     */
    @Override
    public void addCache(Long configId) {
        //加入缓存

        final DictInfoDto dictLabel = sysTreeDictService.findOneByDictLabel(Constant.IDAL_CACHE_FILE_PREFIX);

        final Config config = configService.selectConfigById(configId);
        List<ConfigVersion> list = configVersionService.findByConfigIdAndUserStatus(configId);

        final AvailableConfig availableConfig = new AvailableConfig();
        final List<String> contents = new ArrayList<>();
        for (ConfigVersion version : list) {
            String path = version.getFileUrl();
            String suffix = path.substring(path.lastIndexOf(".") + 1);
            final FileTypeEnum fileTypeEnum = FileTypeEnum.bySubfix(suffix);
            // 校验文件是否为相同类型
            if (Objects.isNull(availableConfig.getTypeEnum())) {

                availableConfig.setTypeEnum(fileTypeEnum);
            } else {
                if (fileTypeEnum.compareTo(availableConfig.getTypeEnum()) != 0) {
                    continue;
                }
            }
            contents.add(version.getContent());

        }
        availableConfig.setContents(contents);
        //合并配置到缓存服务中

        String cacheKey = dictLabel.getDictValue()+config.getFileEnName();
        if (FileTypeEnum.EXECL.compareTo(availableConfig.getTypeEnum()) == 0) {
            for (String content : availableConfig.getContents()) {

                execlAccessService.addExeclDataCache(cacheKey,ExeclUtil.strToJson(content));
            }
        } else if(FileTypeEnum.INI.compareTo(availableConfig.getTypeEnum()) == 0) {
            for (String content : availableConfig.getContents()) {

                iniAccessService.addIniDataCache(cacheKey, IniUtil.strToJson(content));
            }
        }else if(FileTypeEnum.PROPERTIES.compareTo(availableConfig.getTypeEnum()) == 0) {
            for (String content : availableConfig.getContents()) {

                propertiesAccessService.addPropertiesCache(cacheKey, PropertiesUtil.strToJson(content));
            }
        }

    }

    /**
     * 暴露访问execl内容的方法
     * @param cacheKey 缓存key
     * @param sheet  访问sheet页号
     * @param startRow 开始访问行号
     * @param params 获取的列参数
     * @return
     */
    @Override
    public List<Map> getExeclValue(String cacheKey, Integer sheet, Long startRow, Map<String, Integer> params){
        final DictInfoDto dictLabel = sysTreeDictService.findOneByDictLabel(Constant.IDAL_CACHE_FILE_PREFIX);
        cacheKey = dictLabel.getDictValue()+cacheKey;
         return execlAccessService.getValue(cacheKey,sheet,startRow,params);

    }

    /**
     * 暴露访问Ini内容的方法
     * @param cacheKey 缓存key
     * @param section  ini文件的分段值
     * @return
     */
    @Override
    public Map getIniValue(String cacheKey, String section){
        final DictInfoDto dictLabel = sysTreeDictService.findOneByDictLabel(Constant.IDAL_CACHE_FILE_PREFIX);
        cacheKey = dictLabel.getDictValue()+cacheKey;
        return iniAccessService.getIniSectionContents(cacheKey,section);
    }

    /**
     * 暴露访问properties文件内容方法
     * @param cacheKey 缓存key
     * @param key properties文件内容的参数key值
     * @return
     */
    @Override
    public String getPropertiesValue(String cacheKey, String key){
        final DictInfoDto dictLabel = sysTreeDictService.findOneByDictLabel(Constant.IDAL_CACHE_FILE_PREFIX);
        cacheKey = dictLabel.getDictValue()+cacheKey;
        return   propertiesAccessService.getProperties(cacheKey,key);
    }

    /**
     * 暴露访问properties文件内容方法
     * @param cacheKey 缓存key
     * @param key properties文件内容的参数key值
     * @return
     */
    @Override
    public List<String> getProperties(String cacheKey, String key){
        final DictInfoDto dictLabel = sysTreeDictService.findOneByDictLabel(Constant.IDAL_CACHE_FILE_PREFIX);
        cacheKey = dictLabel.getDictValue()+cacheKey;
        return propertiesAccessService.getPropertiesList(cacheKey,key);
    }
    /**
     * 缓存模糊删除
     * @param cacheKey
     * @return
     */
    @Override
    public Boolean fuzzyDelKeyAll(String cacheKey){
     return  iniAccessService.fuzzyDelKeyAll(cacheKey);
    }
}
